package com.alibaba.citrus.ots.forecast.facade.service;

import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.forecast.repository.DealLineQueryRepository;
import com.alibaba.cz.base.tool.Nullable;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.bcots.model.dto.EmployeeGetCurrentLoginResponse;
import com.epoch.app.otsforecastsales.api.deallinequeuequery.service.DealLineQueueQueryService;
import com.epoch.app.otsforecastsales.domian.deallinequeue.model.DealLineQueue;
import com.epoch.app.otsforecastsales.dto.response.QueryProgressQueueResult;
import com.epoch.app.otsforecastsales.model.dto.DealLineSDO;
import com.epoch.app.otsforecastsales.model.enums.ForecastDealTypeSEO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author yuanDeng
 * @Date 2021/10/19 15:09
 **/
@Service
@Primary
class DealLineQueueQueryServiceImpl implements DealLineQueueQueryService {
    @Resource
    DealLineQueryRepository dealLineQueryRepository;

    @FacadeInvoker
    @Override
    public Result<List<QueryProgressQueueResult>> queryProgressQueueResult() {
        List<QueryProgressQueueResult> queryProgressQueueResults=new ArrayList<>();
        EmployeeGetCurrentLoginResponse employeeGetCurrentLoginResponse = queryProgressQueueResult$callAPI(new Object());
        DealLineSDO dealLineSDO=new DealLineSDO();
        //dealLineSDO.setCreator(employeeGetCurrentLoginResponse.getEmployeeId());
        dealLineSDO.setDealType(ForecastDealTypeSEO.BATCH_ANALYSIS.getDealTypeCode().toString());//分析
        Result<List<DealLineQueue>> listResult1 = dealLineQueryRepository.queryProgressQueueResult(dealLineSDO);
        QueryProgressQueueResult queryProgressQueueResult1=new QueryProgressQueueResult();
        queryProgressQueueResult1.setProgressName("批量分析");
        queryProgressQueueResult1.setProgressPercent("0");
        if(!CollectionUtils.isEmpty(listResult1.getResult())&&listResult1.getResult().size()!=0){
            Map<Integer, List<DealLineQueue>> collect = Nullable.stream(listResult1.getResult()).collect(Collectors.groupingBy(DealLineQueue::getDealStatus));
            List<DealLineQueue> dealLineQueues = collect.get(1);
            NumberFormat numberFormat =NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            String percent = numberFormat.format((float) dealLineQueues.size() / (float) listResult1.getTotal() * 100);
            queryProgressQueueResult1.setProgressPercent(percent);
        }

        dealLineSDO.setDealType(ForecastDealTypeSEO.BATCH_CONFIRM.getDealTypeCode().toString());//确认
        Result<List<DealLineQueue>> listResult2 = dealLineQueryRepository.queryProgressQueueResult(dealLineSDO);
        QueryProgressQueueResult queryProgressQueueResult2=new QueryProgressQueueResult();
        queryProgressQueueResult2.setProgressName("批量确认");
        queryProgressQueueResult2.setProgressPercent("0");
        if(!CollectionUtils.isEmpty(listResult2.getResult())&&listResult2.getResult().size()!=0){
            Map<Integer, List<DealLineQueue>> collect = Nullable.stream(listResult1.getResult()).collect(Collectors.groupingBy(DealLineQueue::getDealStatus));
            List<DealLineQueue> dealLineQueues = collect.get(1);
            NumberFormat numberFormat =NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            String percent = numberFormat.format((float) dealLineQueues.size() / (float) listResult1.getTotal() * 100);
            queryProgressQueueResult2.setProgressPercent(percent);
        }

        dealLineSDO.setDealType(ForecastDealTypeSEO.CHECK_ACTIVITIES.getDealTypeCode().toString());//活动检查
        Result<List<DealLineQueue>> listResult3 = dealLineQueryRepository.queryProgressQueueResult(dealLineSDO);
        QueryProgressQueueResult queryProgressQueueResult3=new QueryProgressQueueResult();
        queryProgressQueueResult3.setProgressName("活动检查");
        queryProgressQueueResult3.setProgressPercent("0");
        if(!CollectionUtils.isEmpty(listResult3.getResult())&&listResult3.getResult().size()!=0){
            Map<Integer, List<DealLineQueue>> collect = Nullable.stream(listResult1.getResult()).collect(Collectors.groupingBy(DealLineQueue::getDealStatus));
            List<DealLineQueue> dealLineQueues = collect.get(1);
            NumberFormat numberFormat =NumberFormat.getInstance();
            numberFormat.setMaximumFractionDigits(2);
            String percent = numberFormat.format((float) dealLineQueues.size() / (float) listResult1.getTotal() * 100);
            queryProgressQueueResult3.setProgressPercent(percent);
        }
        queryProgressQueueResults.add(queryProgressQueueResult1);
        queryProgressQueueResults.add(queryProgressQueueResult2);
        queryProgressQueueResults.add(queryProgressQueueResult3);
        return Result.success(queryProgressQueueResults);
    }

    /**
     * queryFABBalloonVisible
     */
    @Override
    @FacadeInvoker
    public Result<Boolean> queryFABBalloonVisible() {
        DealLineSDO dealLineSDO=new DealLineSDO();
        Result<Boolean> result = dealLineQueryRepository.queryFABBalloonVisible(dealLineSDO);
        return result;
    }

    /**
     * 查询处理队列列表
     * @return
     */
    @FacadeInvoker
    public Result<List<DealLineQueue>> queryDealQueue() {
        DealLineSDO dealLineSDO=new DealLineSDO();
        Result<List<DealLineQueue>> listResult = dealLineQueryRepository.queryProgressQueueResult(dealLineSDO);
        return listResult;
    }
}
